import { ElMessage,ElMessageBox } from 'element-plus'
import nprogress from 'nprogress'

//消息提示
export function toast(message,type="success",dangerouslyUseHTMLString=false){
  ElMessage({
    message,
    type,
    plain: true,
    duration:3000,
  })
}

//显示全局loading
export function showFullLoading(){
  nprogress.start
}
//隐藏全局loading
export function hideFullLoading(){
  nprogress.done
}

//弹出提示
export function showModal(content="提示内容",type="warning",title=""){
  return ElMessageBox.confirm(
    content,
    title,
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type,
    }
  )
}

//弹出输入框
export function showPrompt(tip,value = ""){
  return ElMessageBox.prompt(tip, '', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    inputValue:value
  })
}

//将query对象转成url参数
export function queryParams(query){
  let q = []
  for(const k in query){
    if(query[k]){
      q.push(`${k}=${encodeURIComponent(query[k])}`)
    }
  } 
  let r = q.join("&") 
  r = r ? ("?" + r) : ""

  return r
}


//排序 上移  方法
export function useArrayMoveUp(arr,index){
  swapArray(arr,index,index - 1)
}

//排序 下移 方法
export function useArrayMoveDown(arr,index){
  swapArray(arr,index,index + 1)
}

function swapArray(arr,index1,index2){
  arr[index1] = arr.splice(index2,1,arr[index1])[0]
  return arr
}
